﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Model.CSP;
using CSP.Model.CSPEnum;
using CSP.Common.Tools;
using CSP.Web.Master;
using CSP.Web.UserControls;

namespace CSP.Web.MyHome.ConsumeOrder
{
    public partial class Detail : CSPMyHome
    {


        private string _contactWayJson = "";

        /// <summary>
        /// 获取联系方式的前台UI json序列化字符串
        /// </summary>
        protected string ContactWayJson
        {
            get { return _contactWayJson; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindUI();
            }
        }

        private void BindUI()
        {
            if (Request["ordernumber"] == null)
            {
                this.FailRedirect("请所请求的订单不存在", "/MyHome/Account/Management.aspx");
                this.LoggerProvider.Warn(new Exception("传递的URLordernumber参数为空"));
            }
            string orderNumber = Request["ordernumber"].ToString();

            Order order = GetOrdersByOrderNumber(orderNumber);
            if (order == null)
            {
                this.FailRedirect("请所请求的订单不存在", "/MyHome/Account/Management.aspx");
                this.LoggerProvider.Warn(orderNumber, new Exception("请所请求的订单不存在"));
            }

            CSP.Model.CSP.ServiceInfo serviceInfo = GetServiceInfoById(order.ServiceInfoId);
            SetContactWay(serviceInfo.username);
            ICollection<CSP.Model.CSP.ContactWay> contactWay = GetContactWaysByUser(serviceInfo.username);

            this.LB_OrderUserName.Text = order.OrderUserName;
            this.LB_ServiceUserName.Text = order.ServiceUserName;
            this.LB_ConsumePhone.Text = order.Phone;
            this.LB_ServicePhone.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.Telephone == "" ? serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.Mobile
                : serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.Mobile;
            this.LB_ConsumeAddress.Text = order.Address;
            this.LB_ServiceAddress.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.CN_Province + " " + serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.CN_City +
                " " + serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.CN_County + "" + serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.Street;
            this.LB_ServiceMail.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.Email;
            this.LB_OrderNumber.Text = orderNumber;
            this.LB_CooperationStartTime.Text = order.CooperationStartTime != null ? order.CooperationStartTime.Value.ToString("yyyy-MM-dd") : "";
            this.LB_CooperationEndTime.Text = order.CooperationEndTime != null ? order.CooperationEndTime.Value.ToString("yyyy-MM-dd") : "";
            this.LB_OrderConfirmDate.Text = order.OrderConfirmDate != null ? order.OrderConfirmDate.Value.ToString("yyyy-MM-dd") : "";
            this.LB_Remark.Text = order.Remark;
            this.LB_ServiceTitle.Text = order.ServiceTitle;
            this.LB_OrderStatus.Text = GetNameByOrderStatus(order.status);
            this.LB_Description.Text = serviceInfo.Description;
            this.LB_Price.Text = GetPriceByPriceTypeAndPriceAndPriceStartAndPriceEnd((int)serviceInfo.PriceType, serviceInfo.Price, serviceInfo.PriceStart, serviceInfo.PriceEnd);
            this.LB_UnitCount.Text = order.UnitCount.ToString();
            this.LB_TotalPrice.Text = order.TotalPrice.ToString("0.00");
            this.LB_ServiceStatus.Text = GetNameByServiceInfoStatus((int)serviceInfo.status);
            this.IMG_ServiceImage.Src = serviceInfo.ServiceImages.ToArray()[0].FilePath + "S_" + serviceInfo.ServiceImages.ToArray()[0].FileName;
        }

        /// <summary>
        /// 根据订单状态获取订单名称
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <returns></returns>
        private string GetNameByOrderStatus(int orderStatus)
        {
            string result = "";
            if ((OrderStatus)orderStatus == OrderStatus.Waiting)
            {
                result = "预约中";
            }
            else if ((OrderStatus)orderStatus == OrderStatus.Canceled)
            {
                result = "订单取消";
            }
            else if ((OrderStatus)orderStatus == OrderStatus.ServiceConfirm)
            {
                result = "服务者已确认预约";
            }
            else if ((OrderStatus)orderStatus == OrderStatus.ConsumeConfirm)
            {
                result = "预约成功";
            }
            return result;
        }

        /// <summary>
        /// 根据服务信息状态，获取服务信息Id
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        private string GetNameByServiceInfoStatus(int status)
        {
            string result = "未上线";
            if ((ServiceInfoStatus)status == ServiceInfoStatus.NotOnLine)
            {
                result = "未上线";
            }
            else if ((ServiceInfoStatus)status == ServiceInfoStatus.OffLine)
            {
                result = "下线";
            }
            else if ((ServiceInfoStatus)status == ServiceInfoStatus.OnLine)
            {
                result = "上线";
            }
            else if ((ServiceInfoStatus)status == ServiceInfoStatus.Snapshot)
            {
                result = "快照";
            }
            return result;
        }

        /// <summary>
        /// 根据Id获取服务信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private CSP.Model.CSP.ServiceInfo GetServiceInfoById(int id)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            CSP.Model.CSP.ServiceInfo result = null;
            using (IServiceInfoBll serviceInfoBll = bllFactory.GetServiceInfoBll())
            {
                result = serviceInfoBll.GetDataById(id);
            }
            return result;
        }

        /// <summary>
        /// 根据User获取联系方式
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private ICollection<Model.CSP.ContactWay> GetContactWaysByUser(string userName)
        {
            ICollection<Model.CSP.ContactWay> result = null;
            CSPBllFactory factory = CSPBllFactory.GetInstance();
            using (IContactWayBll contactWayBll = factory.GetContactWayBll())
            {
                result = contactWayBll.FindByUser(userName);
            }

            return result;
        }

        /// <summary>
        /// 根据分页信息和Ids获取Orders订单信息
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        private Order GetOrdersByOrderNumber(string orderNumber)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            Order result = new Order();
            using (IOrderBll orderBll = bllFactory.GetOrderBll())
            {
                result = orderBll.GetByOrderNumberAndConsumeUser(orderNumber, UserProvider.UserName);
            }
            return result;
        }

        /// <summary>
        /// 根据价格类型，现价格，最低价格，最高价格，获取价格
        /// </summary>
        /// <param name="priceType"></param>
        /// <param name="price"></param>
        /// <param name="priceStart"></param>
        /// <param name="priceEnd"></param>
        /// <returns></returns>
        private string GetPriceByPriceTypeAndPriceAndPriceStartAndPriceEnd(int priceType, decimal price, decimal priceStart, decimal priceEnd)
        {
            string result = "";
            if ((PriceType)priceType == PriceType.FixedPrice) //一口价
            {
                result = price.ToString("0.00");
            }
            else if ((PriceType)priceType == PriceType.PriceRange)//价格范围
            {
                result = priceStart.ToString("0.00") + "--" + priceEnd.ToString("0.00");
            }
            else if ((PriceType)priceType == PriceType.Negotiable)//面议
            {
                result = "面议";
            }
            return result;
        }

        /// <summary>
        /// 设置联系方式
        /// </summary>
        /// <param name="userName"></param>
        private void SetContactWay(string userName)
        {
            ICollection<Model.CSP.ContactWay> contactWays = GetContactWaysByUser(userName);

            if (contactWays == null || contactWays.Count == 0)
            {
                return;
            }

            ContactWayUI[] contactWayUI = (from contact in contactWays
                                           select new ContactWayUI
                                           {
                                               Address = contact.address,
                                               Type = contact.type
                                           }).ToArray();
            _contactWayJson = CommonTool.SerializeObject(contactWayUI);
            //if (contactWays == null)
            //{
            //    contactWays = new Collection<Model.CSP.ContactWay>();
            //}
            //this.R_ContactWay.DataSource = contactWays;
            //this.R_ContactWay.DataBind();
        }

        private class ContactWayUI
        {
            public string Address { get; set; }

            public int Type { get; set; }
        }
    }
}